﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>废料录入</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <script src="~/js/layui/layui.js"></script>
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button id="btn-update" type="button" name="btn_update" onclick="Confrim()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1005;</i>废料入库
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%;">
            <button id="btn-update" type="button" name="btn_update" onclick="SureAdd()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe61f;</i>确认添加
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%;">
            <button id="btn-sync" type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i> 关闭页面
            </button>
        </li>
    </ul>

    <div class="layui-card" style="margin-top:10px">
        <div class="layui-card-body" style="padding-top:2%">
            <form id="dForm" class="layui-form layui-col-space5">
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            关联计划
                        </div>
                        <input type="text" id="plCode" name="plCode" class="layui-input" style="width:120%" readonly />
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:150px">
                        <div class="layui-input-prefix">
                            生产工序
                        </div>
                        <input type="text" id="gxName" name="gxName" class="layui-input" style="width:120%" readonly />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            关联条码
                        </div>
                        <input type="text" id="barCode" name="barCode" class="layui-input" style="width:120%">
                        <div class="layui-input-suffix" style="cursor: pointer;" onclick="chooseBarCode()">
                            <i class="layui-icon layui-icon-more-vertical"></i>
                        </div>
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:125px">
                        <div class="layui-input-prefix">
                            存货信息
                        </div>
                        <input type="text" id="proName" name="proName" class="layui-input" style="width:140%" readonly />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            废料类型
                        </div>
                        <select id="flType" name="flType" style="width:120%"></select>
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:144px">
                        <div class="layui-input-prefix">
                            废料数量
                        </div>
                        <input type="number" id="number" name="number" class="layui-input" style="width:120%" >
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:144px">
                        <div class="layui-input-prefix">
                            使用完成
                        </div>
                        <select name="useFinish" id="useFinish">
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </div>

                <input type="hidden" id="gxCode" name="gxCode">
                <input type="hidden" id="MaxFlNum" name="MaxFlNum">

            </form>
        </div>
        <div class="layui-card-body " id="layui-table-main" > @*  style="float:left;" *@
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>
    </div>
</body>
</html>
<script src="~/js/sysweb/planjs.js"></script>
<script type="text/html" id="barDemo">
    <a title="删除" href="javascript:;" lay-event="del" ><i class="layui-icon">&#xe640;</i></a>
</script>
<script type="text/javascript">

    var plCode = "", gxCode = "", gxName = "", MaxFlNum="";
    var isInKu = false;
    var finish = "0";
    var nowFl = "0";//已添加废料数量
    $(function () {
        plCode = getUrlVariable("plCode");
        $("input[ name='plCode']").val(plCode);
        finish = getUrlVariable("isFinish");
        gxCode = getUrlVariable("gxCode");
        $("input[ name='gxCode']").val(gxCode);
        gxName = getUrlVariable("gxName");
        $("input[ name='gxName']").val(gxName);
        $("input[ name='number']").val("0");

        MaxFlNum = getUrlVariable("MaxFlNum");  //计划工序最大废料数量
        $("input[ name='MaxFlNum']").val(MaxFlNum);

        CommonData("flType", "flType", "");
        loadData();

    });

    var dUrl = "../Plan/getDataList?dataType=planfl&condition=" + getUrlVariable("plCode") + "," + getUrlVariable("gxCode");

    function loadData() {
        nowFl = "0";
        layui.use('table', function () {
            var table = layui.table;
            var cols = table.render({
                elem: '#list'
                //, data: JSON.parse(datas)
                , url: dUrl
                , skin: 'row' //表格风格
                , even: true
                , height: 'full-200'   //固定高度-即固定表头固定第一行首行
                , page: false
                , limits: [50, 70, 100]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                    { field: 'reBarCode', title: '关联条码', width: 120 }
                    , { field: 'proCode', title: '存货编码', width: 120 }
                    , { field: 'proName', title: '存货名称', width: 140 }
                    , { field: 'flTypeName', title: '废料类型', width: 120 }
                    , { field: 'number', title: '废料数量(kg)', width: 110 }
                    , { field: 'uPrice', title: '单价', width: 110 }
                    , { field: 'createtime', title: '录入时间', width: 180 }
                    , { field: 'userName', title: '录入人员', width: 120 }
                    , { field: 'isIn', title: '是否入库', width: 100 }
                    , { field: 'right', align: 'center', title: '操作', toolbar: '#barDemo', width: 90 }
                ]], done: function (data) {
                 
                    $.each(data['data'], function (i, j) {
                        if (!isInKu && j['orderCode'] != "") {
                            isInKu = true;
                        }

                        nowFl = Number(nowFl) + Number(j['number'])
                        
                    });
                }
            }).config.cols;

            //监听工具条
            table.on('tool(list)', function (obj) {
                var data = obj.data; //当前行旧数据
                var field = obj.field; //单元格字段
                var value = obj.value; //修改后的值
                var event = obj.event; //当前单元格事件属性值
                var update = {};
                update[field] = value;
                //把value更新到行中
                obj.update(update);

                if (event == 'del') {
                    layer.confirm('确定要删除选中数据？', function (index) {
                        var rsl = DelData(data.id);
                        if (rsl.result) {
                            obj.del();
                            layer.msg("删除成功");
                            loadData();
                        } else {
                            layer.msg(info.message);
                        }
                        layer.close(index);

                    });
                }
                
            });


        });

    }

    function chooseBarCode() {
        var url = "../Bar/SelectBarCode?isSingle=1&plCode=" + plCode + "&gxCode=" + gxCode;
        InfoOpenPage(url, "条码选择", "95%", "90%");
    }
   
    function getBarCodePar(pars) {
        if (pars != undefined) {
            $("input[ name='barCode']").val(pars[0].barCode);
            $("input[ name='proName']").val(pars[0].proName + "(" + pars[0].proCode + ")");
        }
    }

    //保存废料信息
    function Confrim() { 
        // console.log(isInKu);
        if (isInKu) {
            layer.msg("当前工序废料已入库");
            return;
        }

        var tableData = new Array();  //列表数据

        layui.use(['table'], function () {
            var table = layui.table
            tableData = table.cache['list']

            if (tableData.length <= 0) {
                layer.msg("没有废料信息");
                return;
            }

        });

        
        layer.confirm('继续操作将会对废料入库,同一计划工序不允许重复入库，是否继续？', function (index) {
            //生成其他入库单
            $.ajax({
                url: "../Plan/SavePlanWasteAsRd08",
                data: { datas: JSON.stringify(FormToJson("dForm")) },
                dataType: "json",
                type: "POST",
                async: false,
                success: function (data) {
                    //console.log(data)
                    // if (data.result) {
                    //     layer.alert("保存成功", { icon: 6 });
                    // } else {
                    //     layer.msg(data.message);
                    // }
                    layer.msg(data.message);
                    loadData();
                }
            });
            layer.close(index);
        });
        
    }

    // 添加废料信息
    function SureAdd() {
        if (finish != "0") {
            layer.msg("工序已完工，禁止录入废料");
            return;
        }
        var barCode = $("#barCode").val();

        if (barCode == "") {
            layer.msg("请先选择关联条码");
            return;
        }


        var total = Number(nowFl) + Number($("#number").val());

        // console.log(Number(nowFl))
        // console.log(Number($("#number").val()))
        // console.log(total)
        // console.log(Number(MaxFlNum))

        if (total > Number(MaxFlNum)) {
            //layer.msg("保存后当前工序废料录入总数量（" + total + "）将大于工序最大废料数量（" + MaxFlNum + "）");
            layer.confirm('保存后当前工序废料录入总数量（' + total + '）将大于工序最大废料数量（' + MaxFlNum + '），是否继续？', function (index) {
                AddPlanWaste()

                layer.close(index);
            });
        }else{
            AddPlanWaste()
        }

        

       
    }

    function AddPlanWaste() {
        $.ajax({
            url: "../Plan/AddPlanWaste",
            data: { datas: JSON.stringify(FormToJson("dForm")) },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (data) {
                if (data.result) {
                    loadData();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

    /**
    * 删除废料信息
    * **/
    function DelData(dataId) {
        var info = undefined;
        $.ajax({
            url: "../Plan/DelPlanWaste",
            data: { dataId: dataId },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (rsl) {
                info = rsl;
            }
        });
        return info
    }

   



</script>

